<template>
  <view>
    <view v-if="!msg_nav.list" :style="'--color:' + color + ';--rgbcolor:' + rgbcolor" class="topbar">
      <text class="item active">
        <text class="span">消息</text>
      </text>
    </view>
    <view v-else :style="'--color:' + color + ';--rgbcolor:' + rgbcolor" class="topbar">
      <text v-for="(item, index) in msg_nav.list" :key="index"
            :class="'item ' + (currentTab === item.var ? 'active' : '')"
            @tap="topbarFn(item.var)">
        <text class="span">{{ item.title }}</text>
        <text v-if="item.newnum > 0" class="b"></text>
      </text>
    </view>
    <block v-if="currentTab === 'sx'">
      <view v-if="globalUtils.empty(CHAT)" class="nodata">
        <image mode="aspectFit" src="/static/res/img/nodata_msg.png"></image>
        <block v-if="iflogin">暂时还没有人和你聊天</block>
        <navigator v-else :style="'--color:' + color + ';--rgbcolor:' + rgbcolor + ';padding:0 100rpx'"
                   class="btn5 ico" hoverClass="none"
                   url="/pages/login/login">
          <text> 登录查看精彩</text>
        </navigator>
      </view>
      <view v-if="!globalUtils.empty(CHAT)" class="msg">
        <view v-if="!ifsubscribe" :style="'--color:' + color + ';--rgbcolor:' + rgbcolor" class="gzhbox"
              @tap="subscribeFn">
          <view class="text">
            <text class="ico"></text>
            关注公众号，接收会员消息提醒~
          </view>
          <view :style="'background:' + color" class="btn3">立即关注</view>
        </view>
        <view v-for="(item, index) in CHAT" :key="index" :data-id="item.id" :data-idx="index" class="dl">
          <navigator :url="'/pagesA/u/u?uid=' + item.uid" class="dt">
            <image :src="item.photo_s_url" mode="aspectFill"></image>
            <text v-if="item.newnum > 0" class="new">{{ item.newnum }}</text>
          </navigator>

          <navigator :url="'/pagesA/chat/chat?uid=' + item.uid" class="dd">
            <view class="h4">
              <image :src="item.grade_ico" class="ugrade1"></image>
              <text class="font">{{ item.nickname }}</text>
            </view>
            <view class="h6">{{ item.content }}</view>
          </navigator>

          <view class="span">{{ item.addtime }}</view>
        </view>
      </view>
    </block>

    <block v-else-if="currentTab === 'tz'">
      <view v-if="globalUtils.empty(TZ)" class="nodata">
        <image mode="aspectFit" src="/static/res/img/nodata_msg.png"></image>
        <block v-if="iflogin">暂无通知信息</block>
        <navigator v-else :style="'--color:' + color + ';--rgbcolor:' + rgbcolor" class="btn5 ico"
                   hoverClass="none" url="/pages/login/login">
          <text> 登录查看</text>
        </navigator>
      </view>
      <view v-if="!globalUtils.empty(TZ)" class="msg">
        <view v-if="!ifsubscribe" :style="'--color:' + color + ';--rgbcolor:' + rgbcolor" class="gzhbox"
              @tap="subscribeFn">
          <view class="text">
            <text class="ico"></text>
            关注公众号，接收会员消息提醒~
          </view>
          <view :style="'background:' + color" class="btn3">立即关注</view>
        </view>
        <view v-for="(item, index) in TZ" :key="index" :data-id="item.id" :data-idx="index" class="dl" @tap="detailFn">
          <view class="dt">
            <view class="i ico k1"></view>
            <text v-if="item.new == 1" class="b"></text>
          </view>

          <view class="dd">
            <view class="h4">{{ item.title }}</view>
            <view class="h6">{{ item.content }}</view>
          </view>

          <view class="span">{{ item.addtime_str }}</view>
        </view>
      </view>
      <view v-if="detail" :class="'mask2 ' + mask_class" data-maskid="detail" @tap="mask_closeFn">
        <view class="div_box" style="--div_box_width: 600rpx; --div_box_height: 566rpx" @tap.stop.prevent="emptyFn">
          <view class="closebtm ico" data-maskid="detail" @tap.stop.prevent="mask_closeFn"></view>
          <view class="h1">消息通知</view>
          <view class="C S15">
            <view style="margin-bottom: 20rpx; border-bottom: #f5f5f5 1px solid; padding-bottom: 20rpx">{{
                detail_title
              }}
            </view>
            <view style="text-align: left">{{ detail_content }}</view>
          </view>
        </view>
      </view>
    </block>

    <block v-else-if="currentTab === 'gg'">
      {{ ARR.length}}
      <view v-if="ARR.length > 0" class="news">
        <navigator v-for="(item, index) in ARR" :key="index" :url="'/pagesA/about/about?t=newsdetail&id=' + item.id"
                   hoverClass="none">
          {{ item.title }}

          <text>{{ item.addtime }}</text>
        </navigator>
        <view class="completed">已全部加载完毕</view>
      </view>

      <view v-else class="nodata">
        <image mode="aspectFit" src="/static/res/img/nodata_msg.png"></image>
        <view>暂无平台公告</view>
      </view>
    </block>
    <view v-if="subscribebox" :class="'mask2 ' + mask_class" data-maskid="subscribebox" @tap="mask_closeFn">
      <view class="div_box" style="--div_box_width: 600rpx; --div_box_height: 666rpx" @tap.stop.prevent="emptyFn">
        <view class="closebtm ico" data-maskid="subscribebox" @tap.stop.prevent="mask_closeFn"></view>
        <view class="h1">长按识别关注公众号</view>
        <view class="kf_wxpic">
          <image :showMenuByLongpress="true" :src="wx_gzh_ewm"></image>
        </view>
        <view class="kf_text">
          <text class="ico dui"></text>
          消息互换提醒　
          <text class="ico dui"></text>
          谁看过我提醒
        </view>
      </view>
    </view>
    <privacy-popup @agree="privacy_agree" @disagree="privacy_disagree"></privacy-popup>
  </view>
</template>
<script>
import privacyPopup from '../../components/privacyPopup/privacyPopup';
import globalUtils from "../../res/www.a9a.cn";

var a = require('../../@babel/runtime/helpers/defineProperty');
var t = require('../../res/www.a9a.cn.js');
var app = getApp();
var n = uni.getStorageSync('COOK_U');
export default {
  computed: {
    globalUtils() {
      return globalUtils
    }
  },
  components: {
    privacyPopup
  },
  data() {
    return {
      currentTab: 'sx',
      detail_flag: false,
      mask_class: 'alpha0_100',
      div_login: false,
      iflogin: false,
      ifonload: false,
      ifsubscribe: false,
      subscribebox: false,
      color: '',
      rgbcolor: '',
      TZ: '',
      MB: '',
      wx_gzh_ewm: '',
      btmnewnum: '',

      msg_nav: {
        list: ''
      },
      ARR: [], // 公告数据
      CHAT: '',
      ifagree: false,
      detail: '',
      detail_title: '',
      detail_content: ''
    };
  },
  onLoad: function () {
    var that = this;
    var n = this;
    app.globalData.get_config('_ZEAI').then(function (a) {
      n.setData({
        color: a.color,
        rgbcolor: t.c16_rgb(a.color)
      });
    });
    app.globalData.chklogin().then(function (t) {
      t
          ? (that.get_data('def'),
              n.setData({
                iflogin: true,
                div_login: false,
                ifonload: true
              }))
          : n.setData({
            iflogin: false,
            ifonload: true
          });
    });
  },
  onShow: function () {
    if (this.ifonload) {
      var that = this;
      app.globalData.chklogin().then(function (t) {
        t
            ? (that.get_data('chat'),
                that.setData({
                  iflogin: true,
                  div_login: false
                }))
            : that.setData({
              iflogin: false,
              div_login: false
            });
      });
    }
  },
  methods: {
    topbarFn: function (a) {
      if (this.iflogin) {
        var t = a;
        console.log('a', a)
        this.setData({
          currentTab: t
        });
        if ('tz' === t) {
          this.get_data('tz');
        } else if ('gg' === t) {
          this.getAnnouncement()
        } else {
          if ('hh' === t) {
            uni.navigateTo({
              url: '/myPages/hh/hh?t=2'
            });
          } else {
            'fk' === t
                ? uni.navigateTo({
                  url: '/myPages/browse/browse?t=1'
                })
                : (this.get_data('chat'));
          }
        }
      } else {
        this.setData({
          iflogin: false
        });
      }
    },
    // 获取公告消息
    getAnnouncement() {
      const that = this;
      app.globalData.ajax({
        url: 'about.php',
        data: {
          submitok: 'news',
          id: null,
        },
        success: function (a) {
          if (1 == a.flag) {
            that.ARR = a.ARR
          } else {
            uni.showToast({
              title: a.msg,
              icon: 'none',
              mask: true
            });
          }
        }
      })
    },
    get_data: function (a) {
      var that = this;
      n = uni.getStorageSync('COOK_U');
      uni.showLoading({
        title: '玩命加载中',
        mask: true
      });
      app.globalData.ajax({
        url: 'my_msg.php',
        data: {
          submitok: a,
          openid: n.openid
        },
        success: function (e) {
          if (1 == e.flag) {
            var n = t.get_tabbar_index('my_msg');
            e.btmnewnum.allnum > 0
                ? uni.setTabBarBadge({
                  index: n,
                  text: e.btmnewnum.allnum
                })
                : uni.removeTabBarBadge({
                  index: n
                });
            var s = 1 == e.subscribe;
            if ('def' == a) {
              a = e.msg_nav.def;
              that.setData({
                currentTab: e.msg_nav.def
              });
            }
            'tz' == a
                ? that.setData({
                  TZ: e.TZ,
                  MB: e.MB,
                  ifsubscribe: s,
                  wx_gzh_ewm: e.wx_gzh_ewm,
                  btmnewnum: e.btmnewnum,
                  msg_nav: e.msg_nav
                })
                : that.setData({
                  CHAT: e.CHAT,
                  MB: e.MB,
                  ifsubscribe: s,
                  wx_gzh_ewm: e.wx_gzh_ewm,
                  btmnewnum: e.btmnewnum,
                  msg_nav: e.msg_nav
                });

          } else {
            that.setData({
              CHAT: []
            });
          }
          uni.hideLoading();
        }
      });
    },

    detailFn: function (i) {
      var that = this;
      t.empty(that.MB) || app.globalData.openSetting(that.MB);
      var o = i.currentTarget.dataset.id;
      var r = i.currentTarget.dataset.idx;
      n = uni.getStorageSync('COOK_U');
      1 == that.TZ[r].new
          ? app.globalData.ajax({
            url: 'my_msg.php',
            data: {
              submitok: 'detail',
              id: o,
              openid: n.openid
            },
            success: function (e) {
              if (1 == e.flag) {
                var n;
                var i = t.get_tabbar_index('my_msg');
                var o = parseInt(that.btmnewnum.allnum);
                var c = parseInt(that.btmnewnum.tznum);
                if (1 == that.TZ[r].new) {
                  if (--o < 0) {
                    o = 0;
                  }
                  if (--c < 0) {
                    c = 0;
                  }
                }
                that.setData(
                    ((n = {
                      detail: true,
                      mask_class: 'alpha0_100',
                      detail_title: that.TZ[r].title,
                      detail_content: that.TZ[r].content
                    }),
                        a(n, 'TZ[' + r + '].new', 0),
                        a(n, 'btmnewnum.allnum', o),
                        a(n, 'btmnewnum.tznum', c),
                        n)
                );
                o > 0
                    ? uni.setTabBarBadge({
                      index: i,
                      text: '' + o
                    })
                    : uni.removeTabBarBadge({
                      index: i
                    });
              } else {
                uni.showToast({
                  title: e.msg,
                  icon: 'none',
                  mask: true
                });
              }
            }
          })
          : that.setData(
              a(
                  {
                    detail: true,
                    mask_class: 'alpha0_100',
                    detail_title: that.TZ[r].title,
                    detail_content: that.TZ[r].content
                  },
                  'TZ[' + r + '].new',
                  0
              )
          );
    },

    mask_closeFn: function (t) {
      var that = this;
      var n = t.currentTarget.dataset.maskid;
      this.setData({
        mask_class: 'alpha100_0'
      });
      setTimeout(function () {
        that.setData(a({}, n, false));
      }, 500);
    },

    emptyFn: function () {
    },

    back: function () {
      this.setData({
        div_login: false
      });
    },

    lawFn: function (a) {
      var t = a.currentTarget.dataset.kind;
      uni.navigateTo({
        url: '/pagesA/about/law?kind=' + t
      });
    },

    subscribeFn: function () {
      this.setData({
        subscribebox: true,
        mask_class: 'alpha0_100'
      });
    },

    getPhoneNumber: function (a) {
      uni.login({
        success: function (i) {
          uni.request({
            url: app.globalData.WWW + '/api/wxxcx/get_openid.php',
            data: {
              code: i.code
            },
            success: function (i) {
              i = t.jsoneval(i.data);
              n = uni.getStorageSync('COOK_U');
              uni.request({
                url: app.globalData.WWW + '/api/wxxcx/get_mob.php',
                data: {
                  session_key: encodeURIComponent(i.U.session_key),
                  encryptedData: encodeURIComponent(a.detail.encryptedData),
                  iv: encodeURIComponent(a.detail.iv),
                  openid: i.U.openid
                },
                success: function (a) {
                  a = t.jsoneval(a.data);
                  uni.reLaunch({
                    url: '/pages/my/my'
                  });
                },
                fail: function (a) {
                  uni.showModal({
                    content: a,
                    showCancel: false
                  });
                }
              });
            }
          });
        }
      });
    },

    ifagreeFn: function (a) {
      1 == a.detail.value
          ? this.setData({
            ifagree: true
          })
          : this.setData({
            ifagree: false
          });
    },

    ifagree_passFn: function () {
      uni.showToast({
        title: '请先点击同意《用户协议》和《隐私条款》',
        icon: 'none',
        mask: true,
        duration: 3000
      });
    },

    privacy_agree: function (a) {
      console.log('agree');
    },

    privacy_disagree: function (a) {
      console.log('disagree');
      uni.exitMiniProgram();
    }
  }
};
</script>
<style>
@import './msg.css';
</style>
